/*
 *  Copyright (C) 2007 Daniel Jackson <carpman@voidptr.org>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA.
 *
 *  Author : Daniel Jackson <carpman@voidptr.org>
*/

using System;
using System.Text;
using System.IO;

namespace PortaMIME
{
    /// <summary>
    /// UNIX MIME type information source. Reads from /etc/mime.types
    /// </summary>
    class UNIXFileSource : TypeDatabaseSource
    {
        #region TypeDatabaseSource Members

        public MIMEType getTypeForExtension(string extension)
        {
        	MIMEType outType = new MIMEType();
        	FileStream mimeTypesFile = File.Open("/etc/mime.types", FileMode.Open, FileAccess.Read);
        	StreamReader mimeTypesReader = new StreamReader(mimeTypesFile);
        	//IRRITANT: sterilize extension, remove leading . if it exists
        	//			I mean, really, how much space could windows save if it didn't include the . in the
        	//			extensions list. It's the little things that count.
        	if(extension.StartsWith("."))
        	{
        		extension = extension.Substring(1);
        	}
        	while(!mimeTypesReader.EndOfStream)
       		{
  				//RANT: THE FRENCH MUSIC HURTS MY BRAIN MEATS!
  				string mimeLine = mimeTypesReader.ReadLine();
  				int spacerBegin = mimeLine.IndexOf('\t');
  				int spacerEnd = mimeLine.LastIndexOf('\t');
  				string mimeType = mimeLine.Substring(0, spacerBegin);
  				string mimeExtensions = mimeLine.Substring(spacerEnd+1);
  				if(mimeExtensions.Contains(extension))
  				{
  					outType.typeString = mimeType;
  				}
       		}
       		return outType;	
        }

        public string[] getExtensionForType(MIMEType type)
        {
            string searchMimeType = type.typeString;
            FileStream mimeTypesFile = File.Open("/etc/mime.types", FileMode.Open, FileAccess.Read);
        	StreamReader mimeTypesReader = new StreamReader(mimeTypesFile);
        	while(!mimeTypesReader.EndOfStream)
        	{
        		string mimeLine = mimeTypesReader.ReadLine();
  				int spacerBegin = mimeLine.IndexOf('\t');
  				int spacerEnd = mimeLine.LastIndexOf('\t');
  				string mimeType = mimeLine.Substring(0, spacerBegin);
  				string mimeExtensions = mimeLine.Substring(spacerEnd+1);
  				if(mimeType == searchMimeType)
  				{
  					return mimeExtensions.Split(' ');
  				}
        	}
        	
        	return new string[] {};
        }

        #endregion
    }
}
